﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using DataEntity;

namespace SWA_View
{
    public partial class SpeedForm : Form
    {
        int _id;
        int _ProjectID;
        public SpeedEntity Entity;
        public SpeedForm(int id, int ProjectID, ExperimentType Type)
        {
            InitializeComponent();
            _id = id;
            _ProjectID = ProjectID;
            if (id > 0)
            {
                CommonModel model = new CommonModel();
                Entity = model.Query<SpeedEntity>("ID = " + id).FirstOrDefault();
                model = null;
            }
            else
            {
                Entity = new SpeedEntity() { ProjectID = _ProjectID, ExperimentType = Type.ToString() };
            }
            SetData(Entity);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            try
            {
                GetData();

            }
            catch (Exception ex)
            {
                MessageBox.Show("请不要输入非数值型字符。");
            }
        }

        private void txtRealLength_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                try
                {
                    double.Parse(tb.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("只能输入数字");
                    tb.Focus();
                    tb.Text = "0";
                }
            }
        }

        private void SetData(SpeedEntity Entity)
        {
            txtSpeed.Text = Entity.Speed + "";
        }

        private SpeedEntity GetData()
        {
            Entity.Speed = float.Parse(txtSpeed.Text);
            return Entity;
        }
    }
}
